package com.cuteboy.admin.service;

import com.cuteboy.dao.SysPermissionMapper;
import com.cuteboy.dao.SysRolePermissionMapper;
import com.cuteboy.entity.SysPermission;
import com.cuteboy.entity.SysRolePermission;
import com.cuteboy.util.ValidateUtils;
import com.cuteboy.vo.ResponseVO;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SysRolePermissionService {
    @Autowired
    @Qualifier("sysRolePermissionMapper")
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    @Qualifier("sysPermissionService")
    private SysPermissionService sysPermissionService;

    public JsonArray listRolePermissionByRoleId(Integer roleId){
        // 获取角色所拥有的权限编码
        List<SysRolePermission> sysRolePermissions = listRolePermiByRoleId(roleId);
        List<String> sysRolePermiCodes = new ArrayList<>();
        for(SysRolePermission sysRolePermission : sysRolePermissions){
            sysRolePermiCodes.add(sysRolePermission.getPermissionCode());
        }

        return listByParentCode("1", sysRolePermiCodes);
    }

    /**
     * 根据上级权限获取下级权限
     * @param parentCode 父级编码
     * @param sysRolePermiIds 已经绑定的权限集合
     * @return
     */
    public JsonArray listByParentCode(String parentCode, List<String> sysRolePermiIds){
        JsonArray resultArray = new JsonArray();
        // 首先获取系统的所有一级权限
        List<SysPermission> sysPermissionList = sysPermissionService.selectByParentCode(parentCode);//得到所有一级菜单
        // 将所有一级权限转为json格式
        JsonArray jsonArray = listToJsonArray(sysPermissionList, sysRolePermiIds);

        for(int i = 0; i < jsonArray.size(); i++){
            JsonObject jsonObject = (JsonObject) jsonArray.get(i);
            //判断该节点下时候还有子节点,有子节点则递归获取子节点，没有则将自己放入结果
            Example example = new Example(SysPermission.class);
            example.or().andEqualTo("parentCode",jsonObject.get("code").getAsString());
            if (sysPermissionService.selectCountByExample(example) != 0) {
                //由于后台模板的规定，一级菜单以title作为json的key
                jsonObject.add("children", listByParentCode(jsonObject.get("code").getAsString(), sysRolePermiIds));
            }
            resultArray.add(jsonObject);
        }
        return resultArray;
    }

    public JsonArray listToJsonArray(List<SysPermission> sysPermissionList, List<String> sysRolePermiIds){
        JsonArray jsonArray = new JsonArray();
        for(SysPermission sysPermission : sysPermissionList){
            JsonObject jsonObject=new JsonObject();
            jsonObject.addProperty("code", sysPermission.getCode()); // 节点id
            jsonObject.addProperty("name", sysPermission.getName()); // 名称
            jsonObject.addProperty("parentCode", sysPermission.getParentCode()); // 父节点

            // 如果角色权限中 包含当前权限，那么checked为选中checked
            if (sysRolePermiIds.contains(sysPermission.getCode())){
                jsonObject.addProperty("checked", "checked");
            }else{
                jsonObject.addProperty("checked", "");
            }

            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    /**
     * 根据角色获取已关联权限
     * @param roleId
     * @return
     */
    public List<SysRolePermission> listRolePermiByRoleId(Integer roleId){
        Example rolePermiExample = new Example(SysRolePermission.class);
        rolePermiExample.or().andEqualTo("roleId", roleId);
        List<SysRolePermission> sysRolePermissions = sysRolePermissionMapper.selectByExample(rolePermiExample);
        return sysRolePermissions;
    }

    /**
     * 保存角色权限
     * @param entity
     * @return
     */
    public SysRolePermission save(SysRolePermission entity){
        sysRolePermissionMapper.insert(entity);
        return entity;
    }

    /**
     * 删除角色所有权限
     * @param roleId
     */
    public void deleteByRoleId(Integer roleId){
        sysRolePermissionMapper.deleteByRoleId(roleId);
    }
}
